<?php
/**
 * 命令模式
 * This file is part of pattern.
 * Author: tangzhou
 * Date: 2016/9/5
 * Time: 17:50
 * File: Observer.php
 */

class Tv
{
    public $currChannel = 0;

    public function turnOn()
    {
        echo "开";
    }

    public function turnClose()
    {
        echo "关";
    }

    public function chanageChannel($channel)
    {
        $this->currChannel = $channel;
        echo $this->currChannel;
    }
}

interface ICommand
{
    public function execute();
}

class CommandOn implements ICommand
{
    /** @var  \Tv */
    private $tv;

    function __construct($tv)
    {
        $this->tv = $tv;
    }

    public function execute()
    {
        $this->tv->turnOn();
    }
}

class CommandClose implements ICommand
{
    /** @var  \Tv */
    private $tv;

    function __construct($tv)
    {
        $this->tv = $tv;
    }

    public function execute()
    {
        $this->tv->turnClose();
    }
}


class CommandChangeChannel implements ICommand
{
    /** @var  \Tv */
    private $tv;

    private $channel;

    function __construct($tv, $channel)
    {
        $this->tv = $tv;
        $this->channel = $channel;
    }

    public function execute()
    {
        $this->tv->chanageChannel($this->channel);
    }
}

/**
 * 控制类
 * Class Control
 */
class Control
{
    private $onCommand;
    private $closeCommand;
    private $changeChanelCommand;

    public function __construct($on, $close, $changeChannel)
    {
        $this->onCommand = $on;
        $this->closeCommand = $close;
        $this->changeChanelCommand = $changeChannel;
    }

    public function trunOn()
    {
        $this->onCommand->execute();
    }

    public function trunClose()
    {
        $this->closeCommand->execute();
    }

    public function changeChannel()
    {
        $this->changeChanelCommand->execute();
    }
}

$tv = new Tv();
$on = new CommandOn($tv);
$close = new CommandClose($tv);
$change = new CommandChangeChannel($tv, 2);

$control = new Control($on, $close, $change);

$control->trunOn();
$control->trunClose();
$control->changeChannel();
